using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Peradigm.Framework.Domain.Collections;
using Peradigm.Framework.Domain.Collections.Generic;
using Peradigm.Framework.Domain.Parties;
using Peradigm.Framework.Persistence;
using Peradigm.Framework.UI.Web;

using Liberty.Domain;
using Liberty.Domain.ApprovalManagement;
using Liberty.Domain.ServiceMemberManagement;

namespace Liberty.UI.ApprovalManagement.Controls
{
    public partial class ReminderListView : AbstractLibertyControl
    {
        #region Constants
        private const string
            SESSION_SORT_DESCRIPTION = "ReminderListSortDescription",
            DEFAULT_SORT_PROPERTY = "OccuredOn",
            FIELD_LABEL_COMPLETED_BY_ID = "lblCompletedBy",
            FIELD_LABEL_COMPLETED_ON_ID = "lblCompletedOn",
            FIELD_LABEL_CONTACT_NUMBER_ID = "lblContactNumber";
        #endregion

        #region Event Handlers
        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !this.IsPostBack )
            {
                this.SortDescription = new SortDescription( DEFAULT_SORT_PROPERTY, SortOrder.Descending );
            }
        }
        protected void gvReminderList_Sorting(object sender, GridViewSortEventArgs e)
        {          
            this.SortDescription.Property = e.SortExpression;
        }
        protected void gvReminderList_RowDataBound( object sender, GridViewRowEventArgs e )
        {
            if ( e.Row.RowType == DataControlRowType.DataRow )
            {
                FollowUpEvent followupEvent = (FollowUpEvent)e.Row.DataItem;
                if ( followupEvent.CompletedBy != null )
                {
                    Label lblCompletedBy = (Label)e.Row.FindControl( FIELD_LABEL_COMPLETED_BY_ID );
                    lblCompletedBy.Text = followupEvent.CompletedBy.Name;
                    Label lblCompletedOn = (Label)e.Row.FindControl( FIELD_LABEL_COMPLETED_ON_ID );
                    lblCompletedOn.Text = followupEvent.CompletedOn.ToString();
                }
                Label lblContactNumber = (Label)e.Row.FindControl( FIELD_LABEL_CONTACT_NUMBER_ID );
                PhoneNumber phoneNumber = followupEvent.Recipient.PhoneNumberFor( ServiceMember.PRIMARY_PHONE_NUMBER );
                if( phoneNumber != null )
                {
                    lblContactNumber.Text = phoneNumber.AsUSString();
                }
            }
        }
        #endregion

        #region Methods
        public override void UpdateView()
        {
            this.SortList( this.Reminders );
            gvReminderList.DataSource = this.Reminders;
            gvReminderList.DataBind();
        }
        #endregion

        #region Properties
        #endregion

        #region Private Methods
        private void SortList( List<FollowUpEvent> reminders )
        {
            reminders.Sort( new Sorter<FollowUpEvent>( this.SortDescription ) );
        }
        #endregion

        #region Private Properties
        private SortDescription SortDescription
        {
            get
            {
                return (SortDescription)this.Session[SESSION_SORT_DESCRIPTION];
            }
            set
            {
                this.Session[SESSION_SORT_DESCRIPTION] = value;
            }
        }
        private List<FollowUpEvent> Reminders
        {
            get
            {
                return (List<FollowUpEvent>)this.Model;
            }
        }
        #endregion

        #region Data Elements
        #endregion

        
}
}
